package com.atguigu.yygh.hosp.controller.admin;


import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.result.ResultCode;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.utils.MD5;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-12-05
 */
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@Api(tags = "医院设置接口")
//@CrossOrigin // 允许所有的目标服务器跨域访问 Access-Control-Allow-Origin: *

public class AdminHospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 查找所有信息
     *
     * @return
     */
    @ApiOperation(value = "查询所有的医院设置的信息")
    @GetMapping("/findAll")
    public R findAll() {
        return R.ok().data("list", hospitalSetService.list());
    }

    /**
     * 根据id删除一个id
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除信息")
    @DeleteMapping("/{id}")
    public R deleteHospitalSetById(@ApiParam(value = "ID", required = true) @PathVariable("id") String id) {
        boolean result = hospitalSetService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }

    /**
     * 插入一个数据
     *
     * @param hospitalSet
     * @return
     */
    @ApiOperation(value = "插入一条数据")
    @PostMapping("/save")
    public R save(@ApiParam(value = "添加一个医院对象", required = true)
                  @RequestBody HospitalSet hospitalSet) {
        if (hospitalSet.getHoscode() == null) {
            return R.error().message("医院编号不能为空");
        }
        try {
            // 设置可用性
            hospitalSet.setStatus(1);
            // 设置签名密钥
            Random random = new Random();
            hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
            boolean result = hospitalSetService.save(hospitalSet);
            if (result) {
                return R.ok().message("添加成功");
            } else {
                return R.error().message("添加失败");
            }
        } catch (DuplicateKeyException e) {
            throw new YyghException(20003, "编号重复", e);
        } catch (Exception e) {
            throw new YyghException(ResultCode.ERROR, "保存失败", e);

        }

    }

    /**
     * 更新一条数据
     *
     * @param hospitalSet
     * @return
     */
    @ApiOperation(value = "更新一条数据")
    @PutMapping("/updateHospSet")
    public R update(@ApiParam() @RequestBody HospitalSet hospitalSet) {
        QueryWrapper<HospitalSet> hospitalSetQueryWrapper = new QueryWrapper<>();
        boolean result = hospitalSetService.updateById(hospitalSet);
        if (result) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }

    }

    @ApiOperation(value = "分页查询列表")
    @GetMapping("/{page}/{limit}")
    public R pageList(
            @ApiParam(value = "当前页", required = true)
            @PathVariable("page") Long page,
            @ApiParam(value = "每页记录数", required = true)
            @PathVariable("limit") Long limit,
            @ApiParam(value = "查询对象", required = true)
            HospitalSetQueryVo hospitalSetQueryVo

    ) {
        Page<HospitalSet> pageParam = hospitalSetService.selectPage(page, limit, hospitalSetQueryVo);
        List<HospitalSet> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        System.err.println(pageParam.getOrders());
        return R.ok().data("rows", records).data("total", total);
    }

    @ApiOperation(value = "根据医院id查询")
    @GetMapping("/getHospSet/{id}")
    public R getById(@ApiParam(value = "需要查询的id", required = true) @PathVariable("id") Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("item", hospitalSet);
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchRemove")
    public R deletePatchHospitalSet(@ApiParam(value = "需要删除的医院id", required = true) @RequestBody List<Long> ids) {
        boolean result = hospitalSetService.removeByIds(ids);
        if (result) {
            return R.ok().message("批量删除成功");
        } else {
            return R.error().message("批量删除失败");
        }
    }

    @ApiOperation(value = "医院状态修改")
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(
            @ApiParam(value = "医院id", required = true)
            @PathVariable("id")
            Long id,
            @PathVariable("status")
            @ApiParam(value = "医院状态", required = true)
            Integer status
    ) {
        if (status != 0 && status != 1) {
            return R.error().message("非法数据");
        }
        HospitalSet hospitalSet = new HospitalSet();
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        boolean result = hospitalSetService.updateById(hospitalSet);
        if (result) {
            return R.ok().message("批量删除成功");
        } else {
            return R.error().message("批量删除失败");
        }
    }

}

